package com.oocl.heather.simpleQQdemo;

import java.awt.Font;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextPane;

@SuppressWarnings("serial")
public class MockClientFrame extends JFrame implements ActionListener{

	private JTextPane jtp;
	private TextField jtf;
	private JButton btn;
	private OutputStream os;
	private InputStream is;
	public MockClientFrame(){
		init();
		prepare();
	}
	@SuppressWarnings("resource")
	private void prepare(){
		try {
			Socket socket=new Socket("localhost",7777);
			os=socket.getOutputStream();
			is=socket.getInputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				BufferedReader br=new BufferedReader(new InputStreamReader(is));
				String line;
				while(true){
					try {
						line = br.readLine();
						jtp.setText(jtp.getText()+"\n"+line);
						System.out.println("jtp"+jtp.getText());
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
			
		}).start();;
		
	}
	
	private void init(){
		jtp=new JTextPane();
		jtf=new TextField();
		btn=new JButton("send");
		add(jtp);
		add(jtf);
		add(btn);
		setSize(800,550);
		setResizable(false);
		setLocationRelativeTo(null);
		setLayout(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		jtp.setBounds(0, 0, 800, 400);
		jtp.setFont(new Font("宋体",2,20));
		jtf.setBounds(0, 400, 550, 100);
		btn.setBounds(550, 400, 250, 100);
		btn.addActionListener(this);
	}
	public static void main(String[] args) {
		new MockClientFrame().setVisible(true);
	}
	public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			String msg=jtf.getText();
			try {
				os.write((msg+"\n").getBytes());
				jtf.setText("");
				System.out.println("message:"+msg);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
	}
